<?php

function get_db_list($db = NULL) {
    if ($data = get_data("SHOW DATABASES", $db)) {
        foreach ($data as $line) {
            if (substr($line['Database'], 0, strlen('__meta_')) == '__meta_')
                continue;
            $ret[] = $line['Database'];
        }

        return $ret;
    } else
        return false;
}

function table_exists($table, $db = NULL) {
    $ret = false;
    if ($data = get_data("SHOW TABLES", $db))
        foreach ($data as $line)
            if (strtolower($table) == strtolower(reset($line)))
                $ret = true;

    return $ret;
}

function get_table_list($db = NULL) {
    if ($data = get_data("SHOW TABLES", $db)) {
        foreach ($data as $line) {
            if (substr(reset($line), 0, strlen('__meta_')) == '__meta_')
                continue;
            $ret[] = reset($line);
        }

        return $ret;
    } else
        return false;
}

function get_fields_info($table, $db = NULL) {
    if ($data = get_data("SHOW COLUMNS FROM `" . $table . "`", $db)) {
        foreach ($data as $line) {
            $ret[] = $line;
        }

        return $ret;
    } else
        return false;
}

function get_fields($table, $db = NULL) {
    if ($data = get_data("SHOW COLUMNS FROM `" . $table . "`", $db)) {
        foreach ($data as $line) {
            $ret[] = $line['Field'];
        }

        return $ret;
    } else
        return false;
}

function get_field_info($table, $field, $db = NULL) {

    if ($data = get_data("SHOW COLUMNS FROM `" . $table . "`", $db)) {
        foreach ($data as $line) {
            if ($line['Field'] == $field) {
                $line['Length'] = get_field_length($line['Type']);
                $line['Type'] = get_field_type($line['Type']);
                return $line;
            }
        }

        return false;
    }

    return false;
}
